<?php
/*
 * Função para formatação de datas;
 */
function formata_data($data,$formato="d/m/Y"){
	if($data != null) { 
		$months = 
				array("january"=>"Janeiro","february"=>"Fevereiro","march"=>"Março","april"=>"Abril","may"=>"Maio","june"=>"Junho","july"=>"Julho","august"=>"Agosto","september"=>"Setembro","october"=>"Outubro","november"=>"Novembro","december"=>"Dezembro");
		$weeks = 
				array("sunday"=>"Domingo","monday"=>"Segunda","tuesday"=>"Terça","wednesday"=>"Quarta","thursday"=>"Quinta","friday"=>"Sexta","saturday"=>"Sábado");
		$months3 = 
				array("jan"=>"jan","feb"=>"fev","mar"=>"mar","apr"=>"abr","may"=>"mai","jun"=>"jun","jul"=>"jul","aug"=>"ago","sep"=>"set","oct"=>"out","nov"=>"nov","dec"=>"dez");
		$weeks3 = 
				array("sun"=>"dom","mon"=>"seg","tue"=>"ter","wed"=>"qua","thu"=>"qui","fri"=>"sex","sat"=>"sab");
		$data = strtolower(date($formato,strtotime($data)));
		$data = strtr($data,$months);
		$data = strtr($data,$weeks);
		$data = strtr($data,$months3);
		$data = strtr($data, $weeks3);
		return $data;
	}
}
/*
 * Função de preenchimento das áreas;
 */
function mostra_areas($parent, $level) { 
   // retrieve all children of $parent 
   $result = pg_query("SELECT id, descricao FROM areas WHERE pai=$parent ORDER BY descricao"); 

   // display each child 
   while ($row = pg_fetch_array($result)) { 
       // indent and display the title of this child 
       echo "<option value='$row[id]'>" . str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;',$level).$row['descricao']."</option>"; 

       // call this function again to display this 
       // child's children 
       mostra_areas($row['id'], $level+1); 
   } 
} 

/*
 * Função para preenchimento de combos;
 */
function preencheCombo($tabela) {
	$result = pg_query("SELECT id, descricao FROM $tabela ORDER BY id");
	while ($row = pg_fetch_array($result)) {
		echo "<option value='".$row[id]."'>".$row['descricao']."</option>";
	}
}

/*
 * Função Anti SQL-Injection;
 */
function anti_injection($string){
  $string = str_ireplace(" or ", "", $string);
  $string = str_ireplace("select ", "", $string);
  $string = str_ireplace("delete ", "", $string);
  $string = str_ireplace("create ", "", $string);
  $string = str_ireplace("drop ", "", $string);
  $string = str_ireplace("update ", "", $string);
  $string = str_ireplace("drop table", "", $string);
  $string = str_ireplace("show table", "", $string);
  $string = str_ireplace("'", "", $string);
  $string = str_replace("#", "", $string);
  $string = str_replace("=", "", $string);
  $string = str_replace("--", "", $string);
  $string = str_replace("-", "", $string);
  $string = str_replace("*", "", $string);
  $string = strip_tags($string);
  $string = addslashes($string);

  return $string;
}

/*
 * Função para tratamento de erros;
 */
function TrataErros($errno, $errstr, $errfile, $errline) {
	switch ($errno) {
		case E_USER_ERROR:
			echo "<b>Erro</b> [$errno] $errstr<br />\n";
			echo "  Erro fatal na linha $errline do arquivo $errfile";
			echo ", PHP " . PHP_VERSION . " (" . PHP_OS . ")<br />\n";
			echo "Interrompendo...<br />\n";
			exit(1);
			break;
		
		case E_USER_WARNING:
			echo "<b>AVISO</b> [$errno] $errstr<br />\n";
			break;
		
		case E_USER_NOTICE:
			echo "<b>Notifica&ccedil;&atilde;o</b> [$errno] $errstr<br />\n";
			break;
		
		case E_NOTICE:
			echo "<b>Erro</b> [$errno] $errstr<br />\n
			Problemas no arquivo $errfile na linha $errline.<br />\n";
			break;
			
		case E_STRICT:
			echo "<b>Erro</b> [$errno] $errstr<br />\n";
			break;
		    
		default:
			echo "Erro desconhecido [$errno] $errstr<br />\n
			Problemas no arquivo $errfile na linha $errline.<br />\n";
			break;
	}
    return true;
}

/*
 * Função para criação dos gráficos via API do Google Charts;
 */
function CriarGrafico ( $min, $max, $largura, $altura, $tipo, $arrays, $titulo="" ) {
	$base_url = "http://chart.apis.google.com/chart?";
	$base_url .= "chds=$min,$max";
	$tipos = array (
		'pizza' => 'p',
		'pizza3d' => 'p3',
		'linha' => 'lc',
		'onda' => 'ls',
		'barra_h' => 'bhs',
		'barra_v' => 'bvs',
		'barra_agrupada' => 'bhg',
		'gauge' => 'gom'
	);
	$base_url .= "&amp;cht=".$tipos[$tipo];
	$base_url .= "&amp;chs=$largura"."x"."$altura";
	$base_url .= ($titulo=="") ? "" : "&amp;chtt=".urlencode($titulo);
	$i = 0;
	foreach ( $arrays as $nome => $valor ) {
		$chd[] = urlencode($valor);
		$chl[] = urlencode($nome." ($valor)");
		$chm[] = ($valor.",000000,0,". $i . ",11");
		$chxlx[] = $nome;
		$i++;
	}
	$base_url .= "&amp;chd=t:".join(",",$chd);
	
	     //$base_url .= ($tipo <> "gauge") ? "&amp;chl=".join("|",$chl) : "";
	$base_url .=($tipo == 'barra_agrupada') ? "&amp;chxt=y" : "";
	$base_url .=($tipo == 'barra_agrupada') ? "&amp;chco=02254d" : "";
	$base_url .= "&amp;chxl=0:|" . join("|",$chxlx) . "";
	$base_url .= ($tipo == 'barra_agrupada') ? "&amp;chm=t".join("|t",$chm)."" : "";
	
	return $base_url;
}

/*
 * Função para a criação das barras de controle dos formul�rios
 */
function controles($save, $delete, $filter, $back, $add, $new) {
	if ($save){
		$save = "<li title='Gravar'><input type='submit' class='ui-state-default ui-corner-all' name='grava' value='Salvar e Enviar Solicita&ccedil;&atilde;o' /></li>";
	}
	if ($delete){
		//$delete = "<input type='submit' class='submit' name='exclui' value='Deletar' />&nbsp;";
		$delete = "<li title='Excluir'><input type='submit' value='Excluir' name='exclui' class='ui-state-default ui-corner-all' /></li>";
	}
	if ($filter){
		$filter = "<li title='Filtrar'><input type='button' class='ui-state-default ui-corner-all' name='filtros' value='Filtros' /></li>";
	}
	if ($back){
		$back = "<li title='Voltar'><input type='button' class='ui-state-default ui-corner-all' name='volta' value='Voltar' onclick='javascript:history.go(-1);' /></li>";
	}
	if ($add){
		$add = "<li title='Adicionar'><input type='button' class='ui-state-default ui-corner-all' name='adiciona' value='Adicionar' onclick=\"javascript:window.location='?aplicativo=$_GET[aplicativo]&amp;subaplicativo=$_GET[subaplicativo]&amp;acao=cadastrar';\" /></li>";
	}
	if ($new){
		$new = "<li title='Novo'><input type='button' class='ui-state-default ui-corner-all' name='novo' id='novo' value='Novo' /></li>";
	}	
	echo "<ul id='icons' class='ui-widget ui-helper-clearfix'>" . $save . $delete . $filter . $back . $add . $new . "</ul>";
}
?>